﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_QuanLyThanhVien : System.Web.UI.Page
{
    QuanTriVienCtr quanTriVienCtr = new QuanTriVienCtr();
    
    public void checkAlreadyLogin()
    {
        if ((Session["UserType"].ToString() != "QuanTri" && Session["UserType"].ToString() != "NhanVien") || Session["UserID"].ToString() == null)
            Response.Redirect("Login.aspx");
    }
    private void loadDuLieu()
    {
        //gvSanPham.DataSource = quanTriVienCtr.layThongTinThanhVienH();
        //gvSanPham.DataBind();
        MultiView1.ActiveViewIndex = 0;
        //MultiView1.ActiveViewIndex = 1;
        lbKQNhanVien.Text = "";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        checkAlreadyLogin();
        if (!IsPostBack)
        {
            
                loadDuLieu();
           
        }

    }
    protected void gvSanPham_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {


            if (e.CommandName.Equals("Edit"))
            {
               
                string tenDN = e.CommandArgument.ToString();
                //string maLoai = gvPhanLoai.SelectedRow.Cells[1].Text;
                DataTable quanTriVien=quanTriVienCtr.layThongTinThanhVienH(tenDN);
                txtCustomerID.Text = quanTriVien.Rows[0]["TenDN"].ToString();
                txtEmail0.Text = quanTriVien.Rows[0]["Email"].ToString();
                txtFullName.Text = HttpUtility.HtmlDecode(quanTriVien.Rows[0]["TenNV"].ToString());
                if (quanTriVien.Rows[0]["Quyen"].ToString() == "QuanTri")
                {
                    ddlchucvu.SelectedValue = quanTriVien.Rows[0]["Quyen"].ToString();
                    ddlchucvu.Enabled = false; btnUpdate.Visible = false;
                }
                else
                {
                    ddlchucvu.SelectedValue = quanTriVien.Rows[0]["Quyen"].ToString();
                }

                MultiView1.ActiveViewIndex = 1;
                
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string chucvu = ddlchucvu.SelectedValue;
        string tendangnhap = txtCustomerID.Text;
        bool flag = quanTriVienCtr.UpdateQuanTriH(chucvu, tendangnhap);
        if (flag)
        {
            Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Cập Nhật Nhân Viên Thành Công');window.location=\"QuanLyThanhVien.aspx\"</script>");

        }
        else
        {
            Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Cập Nhật Nhân Viên Thất Bại');window.location=\"QuanLyThanhVien.aspx\"</script>");
        }
        loadDuLieu();
    }
    protected void bttimkiem_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txttimkiem.Text))
        {
            loadDuLieu();
        }
        else
        {
            DataTable kqNhanVien = quanTriVienCtr.timkiemthanhvien(txttimkiem.Text, Convert.ToInt16(ddlluachon.SelectedValue));
            if (kqNhanVien.Rows.Count > 0)
            {
                gvSanPham.DataSource = kqNhanVien;
                gvSanPham.DataBind();
            }
            else
            {
                lbKQNhanVien.Text = "Không có nhân viên nào";
            }
        }
    }
    
    protected void gvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSanPham.PageIndex = e.NewPageIndex;
        loadDuLieu();
    }
    protected void gvSanPham_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void gvSanPham_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void btThem_Click(object sender, EventArgs e)
    {
        
        string tenNV = txtTenNV.Text;
        string soCMND = txtSoCMND.Text;
        string diaChi = txtDiaChi.Text;
        string soDT = txtSoDT.Text;
        string email = txtEmail.Text;
        string taiKhoan = txtTaiKhoan.Text;
        string matKhau=txtMK.Text;
        if (taiKhoan.Length < 6)
        {
            lbLoiTK.Text = "Tài khoản phải từ 6 ký tự trở lên!";
            return;
        }
        if (matKhau.Length < 6)
        {
            lbLoiMK.Text = "Mật khẩu phải từ 6 ký tự trở lên!";
            return;
        }
        ENhanVien nhanVien = new ENhanVien();
        NhanVienCtr nhanVienCtr=new NhanVienCtr();
        nhanVien.TenNV = tenNV;
        nhanVien.SoCMND = soCMND;
        nhanVien.DiaChi = diaChi;
        nhanVien.SoDienThoai = soDT;
        nhanVien.Email = email;
        nhanVien.TaiKhoan = taiKhoan;
        
        EQuanTriVien quanTriVien = new EQuanTriVien();
        QuanTriVienCtr quanTriVienCtr = new QuanTriVienCtr();
        quanTriVien.TenDN = txtTaiKhoan.Text;
        quanTriVien.MatKhau = txtMK.Text;
        quanTriVien.Quyen = DropDownList1.SelectedValue;
        quanTriVien.LanDNCuoi = DateTime.Parse(DateTime.Now.ToShortDateString());
        quanTriVien.TrangThai = true.ToString();
        bool flag1 = quanTriVienCtr.InsertQuanTri(quanTriVien);
        bool flag = nhanVienCtr.InsertNhanVien(nhanVien);
        if (flag&&flag1)
        {
            Response.Write("<script>alert('Thêm Nhân Viên Thành Công');window.location=\"QuanLyThanhVien.aspx\"</script>");
        }
        else
        {
            Response.Write("<script>alert('Thêm Nhân Viên Thất Bại');window.location=\"QuanLyThanhVien.aspx\"</script>");
        }
            
    }
    protected void btThemMoi_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 2;
    }
}